package cuit.blog.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jakarta.persistence.*;

@Entity
@Table(name = "t_article")
public class Article extends BaseEntity {

    @Column(name = "title")
    private String title;

    @Column(name = "content")
    private String content;
    
    @Column(name = "pub_date")
    private Date pubDate;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER)
    @JoinColumn(name = "articletype_id", referencedColumnName = "id")
    private Articletype articletype;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;
  
    @OneToMany(mappedBy = "article", cascade = {CascadeType.ALL}, orphanRemoval = true)
    private List<Reply> replies = new ArrayList<Reply>();

    // getter 和 setter 方法
    public List<Reply> getReplies() {
        return replies;
    }

    public void setReplies(List<Reply> replies) {
        if (replies == null || replies.isEmpty()) {
            this.replies = new ArrayList<>();
        } else if (this.replies == null || replies.size() != 0) {
            this.replies = new ArrayList<>();
            this.replies.addAll(replies);
        } else {
            this.replies.clear();
            this.replies.addAll(replies);
        }
    }
    public Article() {
    }   
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getPubDate() {
        return pubDate;
    }

    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }

    public Articletype getArticletype() {
        return articletype;
    }

    public void setArticletype(Articletype articletype) {
        this.articletype = articletype;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
	return "Article [title=" + title + ", content=" + content + ", pubDate=" + pubDate + ", articletype="
		+ articletype + ", user=" + user + "]";
    }

}
